package com.neusoft.neuiotms.dcom.converter.problem;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import com.neusoft.bizcore.web.dto.AbstractConverter;
import com.neusoft.neuiotms.dcom.dto.problem.ProblemRegistrationAttachmentDTO;
import com.neusoft.neuiotms.dcom.model.problem.ProblemRegistrationAttachment;
import com.neusoft.neuiotms.dcom.repository.problem.ProblemRegistrationAttachmentRepository;

@Component
public class ProblemRegistrationAttachmentConverter
        extends AbstractConverter<ProblemRegistrationAttachment, ProblemRegistrationAttachmentDTO> {

    @Autowired
    private ProblemRegistrationAttachmentRepository problemRegistrationAttachmentRepository;

    @Override
    public ProblemRegistrationAttachment toModel(final ProblemRegistrationAttachmentDTO dto) {
        ProblemRegistrationAttachment model;
        if (dto.isNew()) {
            model = new ProblemRegistrationAttachment();
        } else {
            model = this.problemRegistrationAttachmentRepository.getOne(dto.getId());
        }
        model.setFsId(dto.getFsId());
        model.setName(dto.getName());
        return model;
    }

    @Override
    public ProblemRegistrationAttachmentDTO toDTO(final ProblemRegistrationAttachment model,
            final boolean forListView) {
        final ProblemRegistrationAttachmentDTO dto = new ProblemRegistrationAttachmentDTO();
        dto.setFsId(model.getFsId());
        dto.setId(model.getId());
        dto.setName(model.getName());
        return dto;
    }

}
